package com.android.demos.module.appbarstyle;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;

import com.aaron.android.framework.base.ui.BaseSwipeBackActivity;
import com.aaron.android.framework.base.widget.viewpager.TabFragmentPagerAdapter;
import com.aaron.android.framework.utils.DisplayUtils;
import com.android.demos.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created on 16/4/21.
 *
 * @author aaron.huang
 * @version 1.0.0
 */
public class AppBarStyleActivity extends BaseSwipeBackActivity {
    public static final int CODE_CUSTOM_1 = 0;
    public static final int CODE_CUSTOM_2 = 1;
    @BindView(R.id.tablayout_app_bar_style)
    TabLayout mTabLayout;
    @BindView(R.id.viewpager_app_bar_style)
    ViewPager mViewPager;
    private Snackbar mSnackbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_view);
        ButterKnife.bind(this);
        initToolbar();
        mTabLayout = (TabLayout) findViewById(R.id.tablayout_app_bar_style);
        mViewPager = (ViewPager) findViewById(R.id.viewpager_app_bar_style);
        TabFragmentPagerAdapter tabFragmentPagerAdapter = new TabFragmentPagerAdapter(this, getSupportFragmentManager(), buildFragmentBinders());
        mViewPager.setAdapter(tabFragmentPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_app_bar_style);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("AppBarStyle");
        final View customView = getLayoutInflater().inflate(R.layout.layout_toolbar_custom_view, null);
        customView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSnackbar = Snackbar.make(customView, "Custom view click", Snackbar.LENGTH_LONG)
                        .setAction("OK", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mSnackbar.dismiss();
                            }
                        });
                Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) mSnackbar.getView();
                snackbarLayout.setAlpha(0.8f);
                mSnackbar.show();
            }
        });

        ActionBar.LayoutParams customLayoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
        customLayoutParams.gravity = Gravity.RIGHT;
        customLayoutParams.rightMargin = DisplayUtils.dp2px(10);
        actionBar.setCustomView(customView,
                customLayoutParams);
        actionBar.setDisplayShowCustomEnabled(true);

    }

    private List<TabFragmentPagerAdapter.FragmentBinder> buildFragmentBinders() {
        List<TabFragmentPagerAdapter.FragmentBinder> fragmentBinders = new ArrayList<>();
        TabFragmentPagerAdapter.FragmentBinder fragmentBinder01 = new TabFragmentPagerAdapter.FragmentBinder(0, "Tab01", 0, new TestFragment01());
        TabFragmentPagerAdapter.FragmentBinder fragmentBinder02 = new TabFragmentPagerAdapter.FragmentBinder(1, "Tab02", 0, new TestFragment02());
        fragmentBinders.add(fragmentBinder01);
        fragmentBinders.add(fragmentBinder02);
        return fragmentBinders;
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}
